/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Bussiness;
import java.awt.event.KeyAdapter;
import java.sql.ResultSet;
import javax.swing.text.JTextComponent;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import java.sql.Statement;
import DatabaseAccess.D_DBHelper;
import java.util.Vector;
import javax.swing.JOptionPane;
/**
 *
 * @author Administrator
 */
public class MJComboBoxKeyListener extends KeyAdapter{

    private Statement st;
    private ResultSet rs;
    private JTextComponent editor;
    private  JComboBox boxSearch;
    public D_DBHelper dbHelper=null;
    public int type;
    public int viewField;
    public int id;
    public Vector vec;
    public Vector vec2;
    public String spName;

    public MJComboBoxKeyListener(){}

    public MJComboBoxKeyListener(Statement st,JComboBox boxSearch){
        this.st=st;
        this.boxSearch=boxSearch;
    }

    public MJComboBoxKeyListener(JComboBox boxSearch){
         dbHelper=new D_DBHelper();
         this.boxSearch=boxSearch;
    }

     public boolean setInformation(String spName,int id,int typeSearch,int viewField){
        try{
            dbHelper.setConnectionUrl("connection.cfg");
        }catch(Exception e){e.printStackTrace();}
        if(!dbHelper.setupConnection()){
             JOptionPane.showMessageDialog(null,"Can not open the connection","Connection Dialog",JOptionPane.ERROR_MESSAGE);
             return false;
        }else{
             this.id=id;
             this.type=typeSearch;
             this.spName=spName;
             this.viewField=viewField;
             return true;
        }
    }
     
    public void keyReleased(KeyEvent e){
         editor=(JTextComponent)boxSearch.getEditor().getEditorComponent();
         if(e.getKeyText(e.getKeyCode()).compareToIgnoreCase("Up")==0||
                  e.getKeyText(e.getKeyCode()).compareToIgnoreCase("Down")==0){
         }else if(editor.getText().equals("")){
                  boxSearch.setPopupVisible(false);
                  boxSearch.removeAllItems();
         }else if(e.getKeyText(e.getKeyCode()).compareToIgnoreCase("Enter")==0){
                 System.out.println("The event press key enter !");
         }else{
                 String text=editor.getText();

                 if(text.contains("'")){
                    text=text.replace("'","\"");
                 }

                 boxSearch.removeAllItems();
                 int rowNumber=0;
                 vec=new Vector();
                 //vec.add(text);
                 vec2=new Vector();
                 vec.add(id);
                 vec.add(text);
                 vec.add(type);

                 System.out.println("ID : "+vec.get(0));
                 System.out.println("Text : "+vec.get(1));
                 System.out.println("Type : "+vec.get(2));

                 try{
                     //rs=dbHelper.executeProcedure("FindProduct",vec);
                     //while(rs.next()){
                        //boxSearch.addItem(rs.getString(2));
                        //rowNumber++;
                      //}
                  }catch(Exception er){
                        er.printStackTrace();
                  }
                  if(rowNumber >15){
                      rowNumber=15;
                  }
                  editor.setText(text);
                  boxSearch.setMaximumRowCount(rowNumber);
                  boxSearch.setPopupVisible(true);
          }
     }
}
